package study.javabasic.Thread;

import java.util.concurrent.CountDownLatch;

/**
 * @author zhangshuhan
 * @date 2019/2/28
 */
public class CountDownLatchTest {
    public static void main(String[] args) {


        CountDownLatch countDownLatch = new CountDownLatch(4);
        TestCountDownLatch testCountDownLatch = new TestCountDownLatch(countDownLatch);
        new Thread(testCountDownLatch, "A").start();
        new Thread(testCountDownLatch, "B").start();
        new Thread(testCountDownLatch, "C").start();
        new Thread(testCountDownLatch, "D").start();


        System.out.println("等待线程执行完毕");
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
     System.out.println("线程集合完毕开始工作");

    }
}


class TestCountDownLatch implements Runnable {

    private CountDownLatch countDownLatch;

    public TestCountDownLatch(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        System.out.println("线程" + Thread.currentThread().getName() + "===开始执行自己的任务");
        try {
            //模拟写入数据的操作
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程" + Thread.currentThread().getName() + "===执行完了任务到达了集合点");
        countDownLatch.countDown();
    }
}
